package com.xwdeshj.controller;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.result.IMoocJSONResult;
import com.xwdeshj.service.XwUserinfoService;
import com.xwdeshj.utils.CookieUtils;
import com.xwdeshj.utils.IMoocExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by BKWJA on 2018/11/19.
 * 用户信息表
 */
@Slf4j
@Controller
@CrossOrigin(allowCredentials = "true")
public class XwUserinfoController extends IMoocExceptionHandler {

    @Autowired
    private XwUserinfoService xwUserinfoService;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public IMoocJSONResult login(String username,String password, HttpServletResponse response){
        return xwUserinfoService.login(username,password,response);
    }

    /**
     * 强制更改密码
     * @param userEncryptid 用户加密id
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @return
     */
    @RequestMapping(value = "/forceModifyPassword")
    @ResponseBody
    public JSONObject forceModifyPassword(String userEncryptid,String oldPwd,String newPwd){
        return xwUserinfoService.forceModifyPassword(userEncryptid,oldPwd,newPwd);
    }

    /**
     *  注册新用户
     * @param username 用户名
     * @param userTel 电话
     * @param userRealname 姓名
     * @param userAvatar 头像
     * @param userSex 性别
     * @param user_role 角色
     * @param password 密码
     * @param userHobbyZt 兴趣爱好（主题游）
     * @param userHobbyYw  兴趣爱好（游玩类型）
     * @param email 邮箱
     * @return
     */
    @RequestMapping("/adduser")
    @ResponseBody
    public IMoocJSONResult addsave(String username, String userTel, String userRealname,
                                   String userAvatar, String userSex, String user_role,String email,
                                   String password,HttpServletRequest request){
        return  xwUserinfoService.addsave(username,userTel,userRealname,userAvatar,userSex,user_role,
                password,email,request);
    }

    /**
     * 用户列表查询
     * @param pagesize  每页显示多少条记录
     * @param pageindex  当前页
     * @param starttime 开始时间
     * @param endtime  结束时间
     * @param searchval   查询关键字
     * @param searchkey  查询字段名
     * @param sortfild  排序字段
     * @param sortway  排序（升序，降序）
     * @return
     */
    @RequestMapping("/querUserList")
    @ResponseBody
    public JSONObject querUserList(@RequestParam(value = "pagesize",defaultValue = "10") Integer pagesize,
                                         @RequestParam(value = "pageindex",defaultValue = "1") Integer pageindex,
                                         @RequestParam(value = "starttime",defaultValue = "") String starttime,
                                         @RequestParam(value = "endtime",defaultValue = "") String endtime,
                                         @RequestParam(value = "searchval",defaultValue = "") String searchval,
                                         @RequestParam(value = "searchkey",defaultValue = "") String searchkey,
                                         @RequestParam(value = "sortfild",defaultValue = "user_createtime") String sortfild,
                                         @RequestParam(value = "sortway",defaultValue = "desc")String sortway,
                                        @RequestParam(value = "user_role",defaultValue = "")String user_role){
        searchval.trim();
        return xwUserinfoService.querUserList(pagesize,pageindex,starttime,endtime,searchval,searchkey,sortfild,sortway,user_role);
    }

    /**
     * 根据用户加密id，查询该用户的个人信息。
     * @param user_encryptId
     * @return
     */
    @RequestMapping("/userInformation")
    @ResponseBody
    public IMoocJSONResult userInformation(String user_encryptId,HttpServletRequest request){
        return  xwUserinfoService.userInformation(user_encryptId,request);
    }


    //修改用户基本资料
    @RequestMapping("/updateInformation")
    @ResponseBody
    public JSONObject updateInformation(String user_encryptId,String user_realName,String user_role,
                                        String user_tel,String user_mail,String user_Avatar,String user_password,
                                        String user_sex,
                                        @RequestParam(value ="farm_encryptId",defaultValue = "") String farm_encryptId,
                                        @RequestParam(value ="sec_encryptId",defaultValue = "")String sec_encryptId,
                                                HttpServletRequest request ){

        return  xwUserinfoService.updateInformation(user_encryptId,user_realName,user_role,user_tel,
                user_mail,user_Avatar,user_password,user_sex,farm_encryptId,
                sec_encryptId,request);
    }

    //删除用户基本资料
    @RequestMapping("/delInformation")
    @ResponseBody
    public JSONObject delInformation(String user_encryptId ,
                                     @RequestParam(value ="farm_encryptId",defaultValue = "") String farm_encryptId,
                                     @RequestParam(value ="sec_encryptId",defaultValue = "")String sec_encryptId,
                                     HttpServletRequest request){
        return  xwUserinfoService.delInformation(user_encryptId,farm_encryptId,sec_encryptId,request);
    }

    /**
     * 搜索用户
     * @param field  搜索字段名
     * @param value 搜索值
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/userquery")
    @ResponseBody
    public JSONObject userquery(String field,String value,HttpServletRequest request, HttpServletResponse response){
        return  xwUserinfoService.userquery(field,value,request,response);
    }


    /**
     *  单个——获取某个用户信息
     * @param user_encryptId 用户加密id
     * @return
     */
    @RequestMapping("/getUserOne")
    @ResponseBody
    public JSONObject getUserOne(@RequestParam(value = "user_encryptId",defaultValue = "")
                                             String user_encryptId,
                              HttpServletRequest request){
        return xwUserinfoService.getUserOne(user_encryptId,request);
    }


    /**
     *  用户——修改兴趣爱好
     * @param user_encryptId  用户加密id
     * @param themeRemarks  主题游备注
     * @param playRemarks  游玩类型备注
     * @param request
     * @return
     */
    @RequestMapping("/modifyUserRemarks")
    @ResponseBody
    public JSONObject modifyUserRemarks(@RequestParam(value = "user_encryptId",defaultValue = "")
                                                  String user_encryptId, String themeRemarks,
                                          String playRemarks,HttpServletRequest request){
        return xwUserinfoService.modifyUserRemarks(user_encryptId,themeRemarks,playRemarks,request);
    }

    /**
     * 用户——修改用户基本信息
     * @param user_encryptId 用户加密id
     * @param updatefiled 修改的字段名
     * @param updateval 修改的内容
     * @param request
     * @return
     */
    @RequestMapping("/updateUserinfo")
    @ResponseBody
    public JSONObject updateUserinfo(@RequestParam(value = "user_encryptId",defaultValue = "")
                                                   String user_encryptId,
                                       String updatefiled,String updateval,HttpServletRequest request){
        updateval = updateval.trim();
        return xwUserinfoService.updateUserinfo(user_encryptId,updatefiled,updateval,request);
    }


    /**
     * 判断是农家乐商家，还是 景区商家
     * @param user_encryptid  登录人加密id
     * @param user_role 登录人用户角色 0：普通用户，1：商家，2：超级管理员
     * @param userid
     * @param request
     * @return
     */
    @RequestMapping("/getuidBusinessType")
    @ResponseBody
    public JSONObject getuidBusinessType(@RequestParam(value = "user_encryptid",defaultValue = "")
                                             String user_encryptid,String user_role,
                                      String userid,HttpServletRequest request){
        return xwUserinfoService.getuidBusinessType(user_encryptid,user_role,userid,request);
    }


    /**
     * 验证用户名是否存在
     * @param username
     * @param request
     * @return
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public JSONObject checkUsername(@RequestParam(value = "username",defaultValue = "") String username,
                                       HttpServletRequest request){
        username = username.trim();
        return xwUserinfoService.checkUsername(username,request);
    }

    /**
     * 验证电话号码是否存在
     * @param tel
     * @param request
     * @return
     */
    @RequestMapping("/checkUsertel")
    @ResponseBody
    public JSONObject checkUsertel(@RequestParam(value = "tel",defaultValue = "") String tel,
                                    HttpServletRequest request){
        tel = tel.trim();
        return xwUserinfoService.checkUsertel(tel,request);
    }

}
